package com.gh.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gh.exception.HospitalException;
import com.gh.model.hosp.HospitalSet;
import com.gh.result.Result;
import com.gh.result.ResultCodeEnum;
import com.gh.service.HospitalSetService;
import com.gh.vo.hosp.HospitalSetQueryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author Eric
 * @Date 2021/6/3 21:12
 * @Version 1.0
 */
@RestController
@RequestMapping("/admin/hosp/hospitalSet")
//@CrossOrigin
public class HospitalSetController {
    @Autowired
    private HospitalSetService hospitalSetService;

    /**
     * 查询医院设置所有信息
     */
    @GetMapping("findAll")
    public Result findAllHospitalSet(){
        List<HospitalSet> list = hospitalSetService.list();
        return Result.ok(list);
    }
    /**
     * 根据id删除
     */
    @DeleteMapping("removeById/{id}")
    public Result deleteHospitalSetById(@PathVariable Long id){
        boolean flag = hospitalSetService.removeById(id);
        if(flag){
            return Result.ok();
        }
        return Result.fail();
    }
    /**
     * 条件查询带分页
     */
    @PostMapping("findHospitalSetList/{page}/{limit}")
    public Result findHospitalSetList(@PathVariable Long page,
                                      @PathVariable Long limit,
                                      @RequestBody(required = false) HospitalSetQueryVo hospitalSetQueryVo){
        Page<HospitalSet> pageHospitalSet=new Page<>(page,limit);

        hospitalSetService.findHospitalSetList(pageHospitalSet,hospitalSetQueryVo);
        pageHospitalSet.getRecords();
        Map<String,Object> map=new HashMap<>();
        map.put("total",pageHospitalSet.getTotal());
        map.put("records",pageHospitalSet.getRecords());
        return Result.ok(map);
    }
    /**
     * 添加医院设置接口
     */
    @PostMapping("saveHospitalSet")
    public Result saveHospitalSet(@RequestBody HospitalSet hospitalSet){
        Boolean flag = hospitalSetService.addHospitalSet(hospitalSet);
        if(flag){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }
    /**
     * 根据id查询医院设置接口
     */
    @GetMapping("getHospitalSetById/{id}")
    public Result getHospitalSetById(@PathVariable Long id){
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        return Result.ok(hospitalSet);
    }
    /**
     * 修改医院设置接口
     */
    @PutMapping("updateHospitalSet")
    public Result updateHospitalSet(@RequestBody HospitalSet hospitalSet){
        boolean flag = hospitalSetService.updateById(hospitalSet);
        if(flag){
            return Result.ok();
        }
        return Result.fail();
    }
    /**
     * 批量删除医院设置接口
     */
    @DeleteMapping("batchRemoveHospitalSet")
    public Result batchRemoveHospitalSet(@RequestBody List<Long> list){
        boolean flag = hospitalSetService.removeByIds(list);
        if(flag){
            return Result.ok();
        }
        return Result.fail();
    }
    /**
     * 医院设置锁定和解锁
     */
    @PutMapping("lockHospitalSet/{id}/{status}")
    public Result lockHospitalSet(@PathVariable Long id,
                                  @PathVariable Integer status){
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        hospitalSet.setStatus(status);
        boolean flag = hospitalSetService.updateById(hospitalSet);
        if(flag){
            return Result.ok();
        }
        return Result.fail();
    }

    /**
     * 发送签名密钥
     */
    @GetMapping("sendKey/{id}")
    public Result sendKey(@PathVariable Long id){
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        String hoscode = hospitalSet.getHoscode();
        String signKey = hospitalSet.getSignKey();
        //TODO
        return Result.ok();
    }
}

